/************************************************************************************/
/*  Program name CAPAMEIMP_RULES_USAGES.sql					                  	    */
/*  Creater:  CHAN Billy (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 10 FEB, 2006   CHAN Billy        First created                                   */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAMEIMP_RULES_USAGES';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_AME_RULES;
		
	lv_COAID				NUMBER;
	lv_Item_ID			NUMBER;
	
BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN
			
			/*SELECT AME_RULES_S.NEXTVAL
			INTO lv_Item_ID
			FROM DUAL;*/
						
			INSERT INTO AME_RULES
			VALUES
			(
			C1.ITEM_ID,
			C1.USAGE_TYPE,
			CAPBRIMP_MAPPYING.GetAMERuleIDMapping(C1.RULE_ID),
			C1.CREATED_BY,
			C1.CREATION_DATE,
			C1.LAST_UPDATED_BY,
			C1.LAST_UPDATE_DATE,
			C1.LAST_UPDATE_LOGIN,
			C1.START_DATE,
			C1.END_DATE,
			C1.SECURITY_GROUP_ID,
			C1.PRIORITY
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;
	
	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/